package linker.list;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;
import java.util.Vector;
import javax.swing.JOptionPane;

import linker.account.Account;
import linker.account.ManageAccount;
import linker.plugin.PluginItem;
import linker.plugin.ReadClassFromFile;

/**
 * 
 * @version linker 0.93
 * @author Jianfeng jianfeng.linker@gmail.com
 * 
 */
public class FunctionList extends Model {
	// @_@
	private static final long serialVersionUID = 1L;

	private Vector<ListItem> list = new Vector<ListItem>();

	private String last = "";

	private Properties functions = new Properties();

	private String functionsInfoPath;

	public FunctionList() throws FileNotFoundException, IOException {

		String path = ManageAccount.getInfoPath();
		String user = Account.getName();
		functionsInfoPath = path + user + ".functions";

		try {
			functions.load(new FileInputStream(functionsInfoPath));
		} catch (FileNotFoundException e) {
			functions.store(new FileOutputStream(functionsInfoPath),
					"Functions");
		}

		Set<String> set = functions.stringPropertyNames();
		for (String title : set) {
			String itemPath = functions.getProperty(title);
			list.add(new FunctionItem(title, itemPath));
		} 

		refilter(null);
	}

	public void addPlugin(PluginItem item) {
		System.out.println("FunctionList addPlugin " + item.getTitle());
		list.add(item);

		refilter(null);
	}

	public void loadPlugins() {
		String pluginPath = ManageAccount.getPluginPath();
		File pluginDir = new File(pluginPath);

		if (pluginDir.exists()) {

			File[] files = pluginDir.listFiles();
			ReadClassFromFile rcff = new ReadClassFromFile();

			for (int t = 0; t < files.length; t++) {
				final File file = files[t];
				if (!file.isDirectory())
					continue;

				String classPath = file.getAbsolutePath()
						+ ManageAccount.getFileSeparator() + file.getName()
						+ ".class";

				try {
					final PluginItem item = (PluginItem) rcff.getClass(
							classPath, file.getName()).newInstance();

					new Thread() {
						public void run() {
							System.out.println("Initial plugin "
									+ file.getName());
							item.setPluginDir(file.getAbsolutePath());
							
							addPlugin(item);
						}
					}.start();

				} catch (InstantiationException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public void addElement(String title) {
		String path = JOptionPane.showInputDialog("Please input a name");

		if (path != null) {
			list.add(new FunctionItem(title, path));
			functions.put(title, path);
		}

		try {
			functions.store(new FileOutputStream(functionsInfoPath),
					"functions");
		} catch (IOException e) {
			e.printStackTrace();
		}

		refilter(null);
	}

	public void refilter(String s) {

		if (s != null)
			last = s;
		else
			s = last;

		ListItem listItem;
		clear();

		for (int i = 0; i < list.size(); i++) {
			listItem = list.elementAt(i);

			if (listItem.toString().indexOf(s) >= 0)
				addElement(listItem);
		}
	}

	public void removeItemByTitle(String lastTitle) {
		list.clear();

		Set<String> set = functions.stringPropertyNames();
		for (String title : set) {
			String itemPath = functions.getProperty(title);
			list.add(new FunctionItem(title, itemPath));
		}
		functions.remove(lastTitle);
	}

	public void addItemWithCommand(String title, String command) {
		list.add(new FunctionItem(title, command));
		functions.put(title, command);

		try {
			functions.store(new FileOutputStream(functionsInfoPath),
					"functions");
		} catch (IOException e) {
			e.printStackTrace();
		}

		refilter(null);
	}

	public void save(String lastTitle, String title, String command) {
		functions.remove(lastTitle);
		functions.put(title, command);

		try {
			functions.store(new FileOutputStream(functionsInfoPath),
					"functions");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
